package com.miaosha.intelligence;

import java.util.Date;

import org.apache.http.client.methods.HttpGet;
import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.miaosha.MediaService;
import com.utils.DateUtil;

/**
 * 秒杀任务创建
 * @author TANGJZ2
 *
 */
public class IntelligenceJob implements Job{
	public static Logger logger = Logger.getLogger(IntelligenceJob.class);
	public static String[] params;
	
	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		logger.info("秒杀程序启动");
		try {
			Date startTime = DateUtil.parse(DateUtil.format(new Date(), "yyyy-MM-dd") + " " + params[2], "yyyy-MM-dd HH:mm:ss.S");
			MediaService.login(params[0], params[1]);//登陆
			MediaService.sign();//签到
			String couponId = MediaService.getCouponId();//获取券ID
			//提前10秒测试网络延迟
			
			//
			HttpGet httpGet = new HttpGet(MediaService.MIAOSHA_URL + couponId);
			MediaService.createSchedule(startTime, httpGet);
		} catch (Exception e) {
			logger.error("秒杀失败", e);
		}
	}

}
